{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solution for </font>Superdense Coding</h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1</h3>\n",
    "\n",
    "Verify the correctness of the above protocol.\n",
    "\n",
    "For each pair of $ (a,b) \\in \\left\\{ (0,0), (0,1), (1,0),(1,1) \\right\\} $:\n",
    "<ul>\n",
    "    <li> Create a quantum curcuit with two qubits: Asja's and Balvis' qubits.</li>\n",
    "    <li> Both are initially set to $ \\ket{0} $.</li>\n",
    "    <li> Apply h-gate (Hadamard) to the first qubit. </li>\n",
    "    <li> Apply cx-gate (CNOT) with parameters first-qubit and second-qubit. </li>\n",
    "</ul>\n",
    "\n",
    "They are separated now.\n",
    "\n",
    "<ul>\n",
    "    <li> If $ a $ is 1, then apply z-gate to the first qubit. </li>\n",
    "    <li> If $ b $ is 1, then apply x-gate (NOT) to the first qubit. </li>\n",
    "</ul>\n",
    "\n",
    "Asja sends her qubit to Balvis.\n",
    "\n",
    "<ul>\n",
    "    <li> Apply cx-gate (CNOT) with parameters first-qubit and second-qubit.</li>\n",
    "    <li> Apply h-gate (Hadamard) to the first qubit. </li>\n",
    "    <li> Measure both qubits, and compare the results with pair $ (a,b) $. </li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "\n",
    "all_pairs = ['00','01','10','11']\n",
    "\n",
    "for pair in all_pairs:\n",
    "\n",
    "    # create a quantum curcuit with two qubits: Asja's and Balvis' qubits.\n",
    "    # both are initially set to |0>.\n",
    "    qreg = QuantumRegister(2) # quantum register with 2 qubits\n",
    "    creg = ClassicalRegister(2) # classical register with 2 bits\n",
    "    mycircuit = QuantumCircuit(qreg,creg) # quantum circuit with quantum and classical registers\n",
    "\n",
    "    # apply h-gate (Hadamard) to the first qubit.\n",
    "    mycircuit.h(qreg[0])\n",
    "\n",
    "    # apply cx-gate (CNOT) with parameters first-qubit and second-qubit.\n",
    "    mycircuit.cx(qreg[0],qreg[1])\n",
    "\n",
    "    # they are separated now.\n",
    "\n",
    "    # if a is 1, then apply z-gate to the first qubit.\n",
    "    if pair[0]=='1': \n",
    "        mycircuit.z(qreg[0])\n",
    "    \n",
    "    # if b is 1, then apply x-gate (NOT) to the first qubit.\n",
    "    if pair[1]=='1': \n",
    "        mycircuit.x(qreg[0])\n",
    "    \n",
    "    # Asja sends her qubit to Balvis.\n",
    "    \n",
    "    #  apply cx-gate (CNOT) with parameters first-qubit and second-qubit.\n",
    "    mycircuit.cx(qreg[0],qreg[1])\n",
    "    \n",
    "    # apply h-gate (Hadamard) to the first qubit.\n",
    "    mycircuit.h(qreg[0])\n",
    "    \n",
    "    # measure both qubits\n",
    "    mycircuit.measure(qreg,creg)\n",
    "    \n",
    "    # compare the results with pair (a,b)\n",
    "    job = execute(mycircuit,Aer.get_backend('qasm_simulator'),shots=100)\n",
    "    counts = job.result().get_counts(mycircuit)\n",
    "    for outcome in counts:\n",
    "        reverse_outcome = ''\n",
    "        for i in outcome:\n",
    "            reverse_outcome = i + reverse_outcome\n",
    "        print(\"(a,b) is\",pair,\"and\",reverse_outcome,\"is observed\",counts[outcome],\"times\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
